function ColaboxDb(){  
    DBNAME= "colaboxdb";
    db=null;      
    if(typeof ColaboxDb.flag == "undefined"){         
        ColaboxDb.prototype.init = function(){          	        
        	    
					  db = google.gears.factory.create('beta.database');    	    
            db.open(DBNAME);
						/*
						rs = db.execute("SELECT count(*) FROM sqlite_master WHERE type='table' and name='colaboxcfg';");				
						if (rs.field(0)==0) {
							  rs.close();							  
							  //init  colaboxcfg
							  
							  db.execute( 'create table if not exists colaboxcfg' +
		             ' (id INTEGER PRIMARY KEY,cfgname text,value text,btime text,desc text)');
		        
		            //init parameters	       
		            //set version
			        	this.setValFromCfg('version','b0.1','ColaBox版本号');
			          this.setValFromCfg('save_time_interval','10','notepad 每次保存的时间间隔');
			          
						}
						rs.close();			
						// init notepad table;
						db.execute(
             'create table if not exists notepad' +
             ' (id INTEGER PRIMARY KEY,title text,content text,btime text,ctime text,syncflag integer,type integer)'
             
             );
						
						*/		
        };
        ColaboxDb.prototype.setValFromCfg=function(name,val,desc){
		        rs = db.execute("select id from colaboxcfg where cfgname='"+name+"'");					
					  if (rs.isValidRow()) {  
					    id=rs.field(0);
					    rs.close();
					    db.execute("update colaboxcfg set value='"+val+"',btime='' where id='"+id+"'");
					}else{
      			  db.execute('insert into colaboxcfg values (?,?,?,?,?)', [null,name,val,null,desc]);
					}				
				
	     };
       ColaboxDb.prototype.getValFromCfg=function(name){
      		rs = db.execute("select value from colaboxcfg where cfgname='"+name+"'");
					t=null;
					if (rs.isValidRow()) {
					
					  t=rs.field(0);
					}
					rs.close();
					
					return t;
					
      };
      ColaboxDb.prototype.dump=function(name){
		    
			   rs = db.execute('select * from '+name);			
			   var str="";
			   while (rs.isValidRow()) {
			  		for (i=0;i<rs.fieldCount();i++){
						   str+=rs.field(i)+'@';
			      }			  
			      str='';
			      rs.next();
			   }
			   rs.close();
      };
      ColaboxDb.prototype.drop=function(name){
      	 db.execute('drop table '+name);
      };
      ColaboxDb.prototype.dropDatabase=function(){
      	db.close(DBNAME);
      	db.execute('drop database '+DBNAME);
      };  
      ColaboxDb.prototype.NodePadSave = function(val){  
      	//alert(val.id+"  "+val.content);
      	
      	//return val.title;
      	if (val.title==""){
      		val.title=null;
      	}
      	
      	var ret;
        if (val.id==null){
        	db.execute('insert into notepad values (?,?,?,?,?,?,?,?,?)', [new Date().getTime(),val.title,val.content,val.btime,null,0,val.type,0,0]);
        	rs=db.execute("select last_insert_rowid();");
        	ret= rs.field(0);
        	rs.close();
        	
        	
        }else{
          db.execute('update notepad  set title=?,content=?,ctime=?,type=?,syncflag=? where id=?', [val.title,val.content,val.btime,val.type,0,val.id]);
          ret=val.id;
          
        } 
        return ret;   
      } 
        
      this.init();
      ColaboxDb.flag = true;  
    }  
} 

var colaboxdb;
var saveWork=function (a, b, message) {	
				
			str="msgid="+message.body[0].msgId;
			var notepadId;
			if  (message.body[0].msgId==0){
				  colaboxdb=new ColaboxDb;
				  google.gears.workerPool.sendMessage([{ret:"true",val:str,msgId:message.body[0].msgId}], message.sender);
			}else if (message.body[0].msgId==1){
				  str+=" id="+message.body[1].id+" title="+message.body[1].title+" type="+message.body[1].type+" content="+message.body[1].content+" btime="+message.body[1].btime;					  
				  notepadId=colaboxdb.NodePadSave(message.body[1]);
				  google.gears.workerPool.sendMessage([{ret:notepadId,val:"save notepad success .",msgId:message.body[0].msgId}], message.sender);
			}
				
				 	
			//str+=" id="+message.body[1].id+" title="+message.body[1].title+" type="+message.body[1].type+" content="+message.body[1].content;	

			
}
google.gears.workerPool.onmessage =saveWork;

